package com.travel.server.manager.impl.command.customer.data;

import com.travel.common.util.CollectionUtil;
import com.travel.common.util.CommonUtil;
import com.travel.common.util.DateUtil;
import com.travel.server.manager.dto.request.customer.data.QueryMemberFansDealRequest;
import com.travel.server.manager.dto.response.customer.data.QueryMemberFansDealResponse;
import com.travel.server.manager.dto.response.customer.data.QueryMemberFansDealResponse.DealItem;
import com.travel.server.manager.impl.command.AbstractCommand;
import com.travel.server.manager.impl.dal.CustomerDal;
import com.travel.server.manager.impl.dal.data.CustomerDataDal;
import com.travel.server.manager.model.MemberGradeInfo;
import com.travel.server.manager.model.dto.data.MemberDealDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @Author: LiZhenhui
 * @Date: 2018/12/10 16:23
 * @Desc:
 */
@Component
@Slf4j
public class QueryMemberFansDealCommand extends AbstractCommand<QueryMemberFansDealRequest, QueryMemberFansDealResponse> {

    @Autowired
    private CustomerDal customerDal;

    @Autowired
    private CustomerDataDal customerDataDal;

    @Override
    protected void checkInput(QueryMemberFansDealRequest request) {
        checkTimeInput(request);
    }

    @Override
    public QueryMemberFansDealResponse handle(QueryMemberFansDealRequest request) {
        checkInput(request);
        Date startTime = DateUtil.getDataTime(request.getStartTime());
        Date endTime = DateUtil.getDataEndTime(request.getEndTime());
        MemberGradeInfo member = customerDataDal.getMember();
        MemberGradeInfo highMember = customerDataDal.getHighMember();
        List<MemberDealDto> list = customerDal.selectMemberDeal(startTime, endTime, member.getId(), highMember.getId());
        return makeResponse(list);
    }

    private QueryMemberFansDealResponse makeResponse(List<MemberDealDto> list){
        QueryMemberFansDealResponse response = new QueryMemberFansDealResponse();
        MemberDealDto memberDealDto = CollectionUtil.getFirst(list);
        Integer memberOldAmount = 0;
        Integer memberNewAmount = 0;
        Integer highMemberOldAmount = 0;
        Integer highMemberNewAmount = 0;
        if(memberDealDto != null){
            memberOldAmount = memberDealDto.getMemberNewAmount();
            memberNewAmount = memberDealDto.getMemberOldAmount();
            highMemberOldAmount = memberDealDto.getHighMemberOldAmount();
            highMemberNewAmount = memberDealDto.getHighMemberNewAmount();
        }
        DealItem member = makeItem("会员", memberNewAmount, memberOldAmount);
        DealItem highMember = makeItem("优质会员", highMemberNewAmount, highMemberOldAmount);
        response.setDealList(Arrays.asList(member, highMember));
        return response;
    }

    private DealItem makeItem(String customerType, Integer newCustomer, Integer oldCustomer){
        DealItem item = new DealItem();
        item.setCustomerType(customerType);
        item.setNewCustomer(CommonUtil.fenToYuan(newCustomer).toString());
        item.setOldCustomer(CommonUtil.fenToYuan(oldCustomer).toString());
        return item;
    }
}
